//
//  MsgNavigationView.m
//  Safflower
//
//  Created by 袁亮 on 2016/12/16.
//  Copyright © 2016年 Migic_Z. All rights reserved.
//

#import "MsgNavigationView.h"

@interface MsgNavigationView()

@property (nonatomic, strong) UIView *cardView;

@end

@implementation MsgNavigationView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self makeMsgNavigationView];
        
    }
    return self;
}

- (void)makeMsgNavigationView
{
    _cardView = [UIView new];
    _cardView.backgroundColor = RGBA(255, 255, 255, 1);
    _cardView.layer.shadowColor = [UIColor blackColor].CGColor;//shadowColor阴影颜色
    _cardView.layer.shadowOffset = CGSizeMake(0,0);//shadowOffset阴影偏移,x向右偏移4，y向下偏移4，默认(0, -3),这个跟shadowRadius配合使用
    _cardView.layer.shadowOpacity = 0.3;
    _cardView.layer.shadowRadius = 1;
    [self addSubview:_cardView];
    [_cardView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.mas_equalTo(0);
        make.left.mas_equalTo(0);
        make.width.mas_equalTo(120);
    }];

    NSArray *titleData = @[@"新的朋友"];
    
    for (int i = 0; i < titleData.count; i++) {

        UIButton *tabButton = [UIButton new];
        tabButton.tag = 100 + i;
        tabButton.titleLabel.font = [UIFont systemFontOfSize:15.0f];
        [tabButton setTitle:titleData[i] forState:UIControlStateNormal];
        [tabButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [tabButton addTarget:self action:@selector(clickTabButton:) forControlEvents:UIControlEventTouchUpInside];
        
        tabButton.selected = i==0 ? YES : NO;
        [self addSubview:tabButton];
        CGFloat top = 0;
        [tabButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(top);
            make.bottom.mas_equalTo(0);
            make.left.mas_equalTo(120*i);
            make.width.mas_equalTo(120);
        }];
    }
    
}

- (void)clickTabButton:(UIButton *)sender
{
    NSInteger index = sender.tag - 100;
    
    [self cardAnimation:index];
    
    [self clearAllButtonSelect];
    
    sender.selected = !sender.selected;
    
    if ([self.delegate respondsToSelector:@selector(clickTabAtIndex:)]) {
        [self.delegate clickTabAtIndex:index];
    }
}


-(void)clearAllButtonSelect
{
    for (id obj in self.subviews) {
        
        if ([obj isKindOfClass:[UIButton class]]) {
            
            UIButton * button = (UIButton*)obj;
            button.selected = NO;
        }
    }
}

- (void)cardAnimation:(NSInteger)index
{
    [_cardView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.mas_equalTo(0);
        make.left.mas_equalTo(120*index);
        make.width.mas_equalTo(120);
    }];
    
    [UIView animateWithDuration:0.1 animations:^{
        
        [self layoutIfNeeded];
    }];
}

- (void)slideTabWithPage:(NSInteger)page
{
    [self cardAnimation:page];
    [self clearAllButtonSelect];
    
    UIButton * button = (UIButton*)[self viewWithTag:page+100];
    
    button.selected = !button.selected;
    
}

@end
